﻿#include "registrationpage.h"
#include "ui_registrationpage.h"
#include<QPushButton>
#include<QFile>
#include<QString>
#include<QIODevice>
#include<QDebug>
#include <QTreeWidgetItem>
#include<QFileDialog>
#include <QByteArray>
#include<QMessageBox>
#include<QDateTime>

registrationPage::registrationPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::registrationPage)
{
    ui->setupUi(this);
    this->setFixedSize(400,200);
    this->setWindowTitle("注册");

    void(registrationPage:: *p1)() = &registrationPage::SaveUser;
    void(registrationPage:: *p2)() = &registrationPage::SavePassword;
    connect(ui->pushButtonlogin,&QPushButton::clicked,this,p1);
    connect(ui->pushButtonlogin,&QPushButton::clicked,this,p2);

    connect(ui->pushButtonBack,&QPushButton::clicked,[=](){
         emit this->back();
    });
}

registrationPage::~registrationPage()
{
    delete ui;
}

void registrationPage::SaveUser()
{
    QString pass1 = ui->lineEditPassword1->text();
    QString pass2 = ui->lineEditPassword2->text();

    if(pass1.compare(pass2) == 0 && (ui->lineEditUser->text()) != NULL)//两次密码不相同2或者User为空";
    {
        QString aFileName="D:/user.txt";
        if (aFileName.isEmpty())
            return;
        saveTextByIODevice(aFileName);
        //注册成功
        //myitme<<"注册成功";
        //emit lan->xinhao();
        QMessageBox::information(this,"提示","注册成功");

        QDateTime time = QDateTime::currentDateTime();

        QString mytime = time.toString("yyyy-MM-dd hh:mm:ss");//"yyyy-MM-dd hh:mm:ss dddd"


        QString pas;
        QFile file1("D:/mylog.txt");
        if (!file1.open(QIODevice::Append | QIODevice::Text))
        {
            //qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pas = file1.readLine();
        }
        //qDebug()<<"密码修改前"<<pas;
        pas="成功注册";
        QByteArray  strBytes=pas.toUtf8();//转换为字节数组
        file1.write(strBytes,strBytes.length());  //写入文件
        pas="\n"+mytime+"\n";
        QByteArray  strBytes1=pas.toUtf8();//转换为字节数组
        file1.write(strBytes1,strBytes1.length());  //写入文件
        //qDebug()<<"密码修改后"<<pas;
        file1.close();
    }else
    {
        //myitme<<"注册失败";
        //emit lan->xinhao();
        QMessageBox::critical(this,"警告","两次密码不相同或用户名为空");

        QDateTime time = QDateTime::currentDateTime();

        QString mytime = time.toString("yyyy-MM-dd hh:mm:ss");//"yyyy-MM-dd hh:mm:ss dddd"


        QString pas;
        QFile file1("D:/mylog.txt");
        if (!file1.open(QIODevice::Append | QIODevice::Text))
        {
            //qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pas = file1.readLine();
        }
        //qDebug()<<"密码修改前"<<pas;
        pas="注册失败";
        QByteArray  strBytes=pas.toUtf8();//转换为字节数组
        file1.write(strBytes,strBytes.length());  //写入文件
        pas="\n"+mytime+"\n";
        QByteArray  strBytes1=pas.toUtf8();//转换为字节数组
        file1.write(strBytes1,strBytes1.length());  //写入文件
        //qDebug()<<"密码修改后"<<pas;
        file1.close();
    }

}

void registrationPage::SavePassword()
{
    QString pass1 = ui->lineEditPassword1->text();
    QString pass2 = ui->lineEditPassword2->text();

    if(pass1.compare(pass2) == 0 && (ui->lineEditUser->text()) != NULL)//两次密码不相同2或者User为空";
    {
        QString aFileName="D:/password.txt";
        if (aFileName.isEmpty())
            return;
        saveTextByIODevice1(aFileName);
    }

}
bool registrationPage::saveTextByIODevice(const QString &aFileName)
{
    //用IODevice方式保存文本文件
    QFile aFile(aFileName);

    if (!aFile.open(QIODevice::Append | QIODevice::Text))
        return false;
    QString str=ui->lineEditUser->text();//整个内容作为字符串
    QByteArray  strBytes=str.toUtf8();//转换为字节数组
    aFile.write(strBytes,strBytes.length());  //写入文件
    str=" ";//整个内容作为字符串
    QByteArray  strBytes1=str.toUtf8();//转换为字节数组
    aFile.write(strBytes1,strBytes1.length());  //写入文件
    aFile.close();
    return true;
}
bool registrationPage::saveTextByIODevice1(const QString &aFileName)
{
    //用IODevice方式保存文本文件
    QFile aFile(aFileName);

    if (!aFile.open(QIODevice::Append | QIODevice::Text))
        return false;
    QString str1=ui->lineEditPassword1->text();//整个内容作为字符串
    QByteArray  strBytes=str1.toUtf8();//转换为字节数组
    aFile.write(strBytes,strBytes.length());  //写入文件
    str1=" ";//整个内容作为字符串
    QByteArray  strBytes1=str1.toUtf8();//转换为字节数组
    aFile.write(strBytes1,strBytes1.length());  //写入文件
    aFile.close();
    return true;
}
